package by.gusto.afisha.fragments;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import by.gusto.afisha.R;
import by.gusto.afisha.model.Category;
import by.gusto.afisha.net.GustoFactory;
import by.gusto.afisha.net.IAbstractFactory;
import by.gusto.afisha.net.ICategoryGrabber;
import by.gusto.afisha.net.ICategoryGrabber.IOnCompliteListener;

import com.actionbarsherlock.app.SherlockFragment;

public class CategoryTitleFragment extends SherlockFragment implements OnItemClickListener, IOnCompliteListener {

	ListView mTableView = null;
	ICategorySelected mCallback;
	IAbstractFactory mFactory = null;
	
	private Handler mHandler = new Handler();
	private View mItemView;
	private LayoutInflater mInflater;
	
	class TitleArrayAdapter<T> extends ArrayAdapter<T> {

		public TitleArrayAdapter(final Context context, final int resource, final int textViewResourceId, final List<T> objects) {
			super(context, resource, textViewResourceId, objects);
		}
		
		@Override
		public View getView(final int position, final View convertView, final ViewGroup parent) {
			View v = super.getView(position, convertView, parent);
			return bindView(position, v);
		}
		
		private View bindView(final int position, final View view) {
			Category category = (Category) getItem(position);
			((TextView) view.findViewById(R.id.title)).setText(category.name);
			((TextView) view.findViewById(R.id.count)).setText(String.valueOf(category.count));
			view.setTag(category);
			return view;
		}
	}
	
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mFactory = new GustoFactory();
	}
	
	@Override
	public void onAttach(final Activity activity) {
		mCallback = (ICategorySelected) activity;
		super.onAttach(activity);
	}
	
	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		mInflater = inflater;
		
		View v = inflater.inflate(R.layout.category_titles, null);
		mTableView = (ListView) v.findViewById(R.id.table);
		mTableView.setOnItemClickListener(this);
		
		ICategoryGrabber catGrabber = mFactory.createCategoryGrabber();
		catGrabber.setOnCompliteListener(this);
		ArrayList<Category> categories = catGrabber.getCategories();
		if (categories != null) {
			mTableView.setAdapter(new TitleArrayAdapter<Category>(getActivity(), R.layout.category_title_item, R.id.title, categories));
		}
		return v;
	}
	
	
	
	public interface ICategorySelected {
		/**
		 * executing when user click on category from list
		 * @return Category
		 */
		void onSelect(Category category);
	}

	@Override
	public void onComplite(final ArrayList<Category> categories) {
		mHandler.post(new Runnable() {
			
			@Override
			public void run() {
				
				List<Category> allCategories = categories;
				if (allCategories.size() == 0) {
					allCategories = Category.getAll();
				}
				mTableView.setAdapter(new TitleArrayAdapter<Category>(getActivity(), R.layout.category_title_item, R.id.title, allCategories));
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int index, long id) {
		if (mCallback != null && index >= 0 && index < Category.getAll().size()) {
			Category c = (Category) view.findViewById(R.id.item_layout).getTag();
			mCallback.onSelect(c);
		}
	}

	
}
